package com.siger.test.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.siger.test.R;

public class MainActivity extends BaseActivity {

    private String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };



    private void testThread() {
        Log.d(TAG, "testThread: " + Thread.currentThread());
//        mThread1.start();
        mThread2.start();
    }


    Thread mThread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            runThread1();
        }
    }, "mThread1");

    Thread mThread2 = new Thread(new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "run: mThread2 start");
            runThread2();
        }
    }, "mThread2");

    private void runThread1() {
        loop();
    }

    private synchronized void runThread2() {
        Log.d(TAG, "runThread2: ");
        loop();
    }

    private void loop() {
        Log.d(TAG, "loop: ");
        for (int i = 0; i < 20; i++) {
            Log.d("runThread1", Thread.currentThread().getName() + " i: " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    public void test(View view) {
        Log.d(TAG, "test: ");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d(TAG, "test run: name " + Thread.currentThread().getName());
                    synchronized (this) {
                        Thread.sleep(5000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "name").start();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Log.d(TAG, "onBackPressed: ");
        mThread1.interrupt();
        mThread2.interrupt();
    }

    public void cropPhoto(Uri uri, int tag) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY It's the ratio of width to height
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY The cut picture is wide
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("return-data", true);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//这里加入flag

        startActivityForResult(intent, tag);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: ");
    }

    public void btnClick1(View view) {
        loop();
    }

    public void ThreadTest(View view) {
        testThread();
    }

    public void btnClickMemLeak1(View view) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() { /* ... */ }
        }, 1000  * 10);

        // Go back to the previous Activity.
        finish();
    }
}
